package com.tianqu.tms.www.proxy.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.ProxyCarInfo;
import com.tianqu.tms.www.proxy.viewhelper.CarProxyInputViewHelper;
/**
 * 管理员解锁结果页
 * 
 * 这块需要涉及很多逻辑
 * 
 * 超期两种状态
 * 
 * 过期两种状态
 * 
 * @author Administrator
 *
 */
public class CarProxyUnchainResultController extends TmsController {
	private Logger logger = Logger.getLogger(CarProxyUnchainResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try{
			controller.begin();
			
			CarProxyInputViewHelper view = (CarProxyInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			
			String serial = view.getSerial();
			
			//这块需要根据时间来判断给予车辆的解锁状态
			ProxyCarInfo proxy = new ProxyCarInfo();
			
			TmsUserProfile profile = uc.getUserProfile();
			Codes code = Codes.getInstance();
			
			int status = code.getItemCodeByItemIdAndItemValue(Codes.PROXYCAR_STATUS,view.getStatus());
			
			if(view.getOutTimeBsflag()!=null && !"".equals(view.getOutTimeBsflag()))
			{
				if("2".equals(view.getOutTimeBsflag()))
				{
					//2：未交费 3:以缴费  4.超期未缴费  5：超期已缴费  6:过期未交费 7过期已交费
					if(status == 2)
					{
						//超期未缴费
						proxy.unchainProxyCarInfoBySerial(serial,4,profile.getUniqueId());
					}
					if(status == 3)
					{
						//超期已交费
						proxy.unchainProxyCarInfoBySerial(serial,5,profile.getUniqueId());
					}
					
				}
				if("4".equals(view.getOutTimeBsflag()))
				{
					if(status == 2 || status == 4)
					{
						//过期未缴费
						proxy.unchainProxyCarInfoBySerial(serial,6,profile.getUniqueId());
					}
					if(status == 3 || status == 5)
					{
						//过期已交费
						proxy.unchainProxyCarInfoBySerial(serial,7,profile.getUniqueId());
					}
				}
			}

			controller.commit();
		}catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");		

	}

}
